var $slide_cover;

$(function() {
    $('input').bind('mouseup', function() {
        $(this).select();
    });
    if ($('.admin_slide').length > 0) {
        $('.slide_post_id').blur(function() {
            $me = $(this);
            get_post_title($me.val(), function(data) {
                $me.parents('tr').find('.slide_content').val(data.post_title);
            });
        });
        $('#slide_update').click(function() {
            var slides = [];
            $('tbody tr').each(function(index) {
                $me = $(this);
                slides[index] = 
                    {
                        'slide_id': index + 1, 
                        'slide_title': $me.find('.slide_title').val(), 
                        'slide_content': $me.find('.slide_content').val(), 
                        'post_id': parseInt($me.find('.slide_post_id').val()),
                        'slide_cover_id': parseInt($me.find('.slide_cover').attr('value'))
                    }
                ;
            });
            $.post('yymin/slide_update', {
                'slides': slides
            }, function(data) {
                if (data != 'error') {
                    alert('成功修改推荐！');
                } else {
                    alert('修改推荐失败！');
                }
            });
        });
        $('#slide_discard').click(function() {
            location.reload();
        });
        $('.slide_cover').click(function() {
            $slide_cover = $(this);
            var post_id = $slide_cover.parents('tr').find('td:nth(1) input').val();
            get_carousel(post_id);
            $('#slide_cover_modal').modal('show');         
            $('#slide_cover_carousel').carousel({
                interval: false
            });
        });
        $('#slide_cover_btn').click(function() {
            var photo_id = $('#slide_cover_carousel .item.active').attr('id');
            $slide_cover.attr('value', photo_id).text('已选择：' + photo_id);
            $('#slide_cover_modal').modal('hide');
        });
        $('#slide_cover_cancel').click(function() {
            $('#slide_cover_modal').modal('hide');
        });
    }
    
    if ($('.admin_feedback').length > 0) {
        $('.fb_handle').click(function() {
            $tr = $(this).parents('tr');
            $.post('yymin/feedback_handle', {
                'fb_id': $tr.find('td:first-child').text()
            }, function(data) {
                if (data != 'error') {
                    $tr.fadeOut(function() {
                        $tr.remove();
                    });
                } else {
                    alert('意见反馈处理失败！');
                }
            });
        });
    }

    if ($('.admin_post').length > 0) {
        $('#post_id').keyup(function() {
            var value = $(this).val();
            if (isNaN(value)) {
                //
            } else {
                get_post_title(value, function(data) {
                    $('#post_title').val(data.post_title);
                });
            }
        });
        $('#post_btn').click(function() {
            $.post('yymin/post_delete', {
                post_id: $('#post_id').val()
            }, function(data) {
                if (data == true) {
                    alert('帖子成功删除！');
                } else {
                    alert('帖子删除失败！');
                }
            });
        });
        $('#cover_btn').click(function() {
        	location.href = (window.EB_DEV ? "/ourplayground" : "") + "/yymin/cover/" + $('#post_id').val();
        });
        $('#repost_id').keyup(function() {
            var value = $(this).val();
            if (isNaN(value)) {
                //
            } else {
                get_repost_content(value, function(data) {
                    $('#repost_content').val(data.repost_content);
                });
            }
        });
        $('#repost_btn').click(function() {
            $.post('yymin/repost_delete', {
                repost_id: $('#repost_id').val()
            }, function(data) {
                if (data == true) {
                    alert('回帖成功删除！');
                } else {
                    alert('回帖删除失败！');
                }
            });
        });        
    }
});

function get_post_title(post_id, callback) {
    $.post('post/info', {
        post_id: post_id,
        cols: [ 'post_title' ]
    }, function(data) {
        callback(data);
    }, 'json');
}

function get_repost_content(repost_id, callback) {
    $.post('repost/info', {
        repost_id: repost_id,
        cols: [ 'extract_repost_content' ]
    }, function(data) {
        callback(data);
    }, 'json');
}

function gen_carousel_indicator(count) {
    var html = '';
    for (i = 0; i < count; i++) {
        html += '<li data-target="#slide_cover_carousel" data-slide-to="' + i + '"';
        if (i == 0) {
            html += 'class="active"></li>';
        } else {
            html += '></li>';
        }
    }
    return html;
}

function get_carousel(post_id) {
    $.post('yymin/slide_cover_carousel', {
        post_id: post_id
    }, function(carousel) {
        $.get('assets/templates/slide_cover_carousel.html', function(html) {
            var carousel_html = '';
            for (id in carousel) {
                var view = {
                    photo_id: carousel[id].photo_id,
                    location: carousel[id].location,
                    width: carousel[id].width,
                    height: carousel[id].height
                };
                carousel_html += Mustache.render(html, view);
            }
            $('#slide_cover_carousel .carousel-inner').empty().html(carousel_html);
            $('#slide_cover_carousel .carousel-inner .item:first-child').addClass('active');
        });    
        var indicator = gen_carousel_indicator(carousel.length);
        $('#slide_cover_carousel .carousel-indicators').empty().html(indicator);        
    }, 'json');
}